package array.prefixsum;

public class NumArray {

    // 前缀和数组
    private int[] preSum;

    // 输入一个数组构造前缀和
    public NumArray(int[] nums) {
        // preSum[0] = 0, 方便计算前缀和
        preSum = new int[nums.length + 1];
        for (int i = 1; i <= nums.length; i++) {
            preSum[i] = preSum[i - 1] + nums[i - 1];
        }
    }

    // 查询区间 [left, right] 的和
    public int sumRange(int left, int right) {
        return preSum[right + 1] - preSum[left];
    }
}
